package org.projectwithoutname.ui.wizard.pages.composite;


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.projectwithoutname.commons.Commons;
import org.projectwithoutname.ui.util.ImageUtil;

public class NameImageComposite extends NameComposite {

	private Text descriptionText = null;
	private Spinner numSeasonsSpin = null;
	private Button browser = null, image = null;
	private Image imageIco = null;
	
	private Label descriptionLabel = null, nSeasons = null;
	private String selectedFile = null;
	private Shell shell;

	
	public NameImageComposite(Composite parent, int style) {
		super(parent, style);
		shell = parent.getShell();
		initialize();
	}

	private void initialize() {
		
		descriptionLabel = new Label(this, SWT.NONE);
		descriptionLabel.setText("Resume:");
		super.putOnScreen(descriptionLabel, 80, 17);
		super.addLine(1, 3);
		descriptionText = new Text(this, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI| SWT.WRAP);
		descriptionText.setTextLimit(2048);
		super.putOnScreen(descriptionText, SWT.None, SWT.FILL, 350, 150, 4, 1);
		
		nSeasons = new Label(this, SWT.None);
		nSeasons.setText("Number of seasons: ");
		super.putOnScreen(nSeasons, 110, 17);
		
		numSeasonsSpin = new Spinner(this, SWT.BORDER);
		numSeasonsSpin.setMinimum(1);
		numSeasonsSpin.setIncrement(1);
		super.putOnScreen(numSeasonsSpin, 18, 17);
		
		super.addLine(1, 2);
		
		
		image = new Button(this, SWT.BORDER_DASH);
		image.setBackground(new Color(getDisplay(), Commons.COLOR_GREY));
		image.setForeground(new Color(getDisplay(), Commons.COLOR_GREY));
		super.putOnScreen(image, SWT.DOWN, SWT.NONE, Commons.SELECTION_WIDTH, Commons.SELECTION_HEIGHT, 3, 3);
		super.addLine(2, 1);
		browser = new Button(this, SWT.NONE);
		browser.setText("Browser");
		browser.setEnabled(false);
		super.putOnScreen(browser, SWT.DOWN, SWT.LEFT, 60, 25);
		browser.addListener(SWT.Selection, new Listener() {
			
			
			@Override
			public void handleEvent(Event event) {
				browser.setEnabled(getTextName().getText().equals("")? false : true);
				String[] filterExtensions = {"*.JPG","*.JPGE","*.PNG"};
				FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
				fileDialog.setText("Select Image");
				fileDialog.setFilterPath("C:/");
				fileDialog.setFilterExtensions(filterExtensions);
				selectedFile = fileDialog.open();
				
				if (selectedFile != null){
					ImageUtil handler = new ImageUtil(selectedFile, shell, getTextName().getText());
					handler.resize(Commons.SELECTION_WIDTH, Commons.SELECTION_HEIGHT);
					imageIco = handler.generate(getDisplay());
					selectedFile = handler.getPath();
					image.setImage(imageIco);
				}
			}
		});
	}
	
	public String imagePath(){
		System.out.println("File: "+selectedFile);
		return selectedFile;
	}
	
	public Image getImageIco(){
		return imageIco;
	}
	
	public Text getDescription(){
		return descriptionText;
	}
	
	public Spinner getNumberOfSeason(){
		return numSeasonsSpin;
	}
	
	public Button selectImage(){
		return browser;
	}

}
